"use strict";
class Vector2d {
    constructor(x = 0, y = 0) {
        this.values = new Float32Array([x, y]);
    }
    /**
     * 构建向量
     * @param x
     * @param y
     */
    static create(x = 0, y = 0) {
        return new Vector2d(x, y);
    }
    /**
     * 向量复制
     * @param from
     * @param to
     */
    static copy(from, to = null) {
        if (to === null) {
            to = new Vector2d();
        }
        to.values[0] = from.values[0];
        to.values[1] = from.values[1];
        return to;
    }
    /**
     * 向量相加
     * @param left
     * @param right
     * @param result
     */
    static sum(left, right, result = null) {
        if (result === null) {
            result = new Vector2d();
        }
        result.values[0] = left.values[0] + right.values[0];
        result.values[1] = left.values[1] + right.values[1];
        return result;
    }
    /**
     * 向量相减
     * @param end
     * @param start
     * @param result
     */
    static difference(end, start, result = null) {
        if (result === null) {
            result = new Vector2d();
        }
        result.values[0] = end.values[0] - start.values[0];
        result.values[1] = end.values[1] - start.values[1];
        return result;
    }
    static scale(direction, scalar, result = null) {
        if (result === null) {
            result = new Vector2d();
        }
        result.values[0] = direction.values[0] * scalar;
        result.values[1] = direction.values[1] * scalar;
        return result;
    }
    /**
     * result = start + direction X scalar;
     * 作用是将一个点（start），沿着direction 给定的方向移动scalar个单位
     * @param start
     * @param direction
     * @param scalar
     * @param result
     */
    static scaleAdd(start, direction, scalar, result = null) {
        if (result === null) {
            result = new Vector2d();
        }
        Vector2d.scale(direction, scalar, result);
        return Vector2d.sum(start, result, result);
    }
    get x() {
        return this.values[0];
    }
    set x(value) {
        this.values[0] = value;
    }
    get y() {
        return this.values[1];
    }
    set y(value) {
        this.values[1] = value;
    }
    get squaredLength() {
        let x = this.values[0];
        let y = this.values[1];
        return (x * x + y * y);
    }
    get length() {
        return Math.sqrt(this.squaredLength);
    }
    /**
     * 标量
     */
    normalize() {
        let len = this.length;
        if (Math.abs(len - 0) <= Vector2d.EPSILOM) {
            this.values[0] = 0;
            this.values[1] = 0;
            return this;
        }
        if (Math.abs(len - 1) <= Vector2d.EPSILOM) {
            return this;
        }
        this.values[0] /= len;
        this.values[1] /= len;
        return this;
    }
    reset(x = 0, y = 0) {
        this.values[0] = x;
        this.values[1] = y;
        return this;
    }
    add(vec) {
        return Vector2d.sum(this, vec, this);
    }
    substract(another) {
        Vector2d.difference(this, another, this);
        return this;
    }
    negative() {
        this.values[0] = -this.values[0];
        this.values[1] = -this.values[1];
        return this;
    }
    equals(vec) {
        if (Math.abs(this.values[0] - vec.values[0]) > Vector2d.EPSILOM) {
            return false;
        }
        if (Math.abs(this.values[1] - vec.values[1]) > Vector2d.EPSILOM) {
            return false;
        }
        return true;
    }
    toString() {
        return `[ x:${this.values[0]} , y:${this.values[1]}]`;
    }
}
Vector2d.EPSILOM = 0.00001;
let vec = new Vector2d(20.1, 10);
console.log(vec.normalize());
